#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QSound>
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    //准备音效
    QSound *startSound = new QSound(":/res/music/TapButtonSound.wav",this);
    //startSound->setLoops(-1);
    //startSound->play();

    //实现退出
    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();
    });
    //设置主页面图片尺寸
    this->setFixedSize(640,1176);
    //设置标题
    this->setWindowTitle("Have a Try?");
    //设置图标
    this->setWindowIcon(QPixmap(":/res/image/Coin0001.png"));
    //创建第二个选关场景
    this->chooseScene=new ChooseLevelScene;



    //创建开始按钮
    MyPushButton * startBtn=new MyPushButton(":/res/image/MenuSceneStartButton.png");
startBtn->setParent(this);
startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.8);
connect(startBtn,&MyPushButton::clicked,[=](){
    //播放音效
    startSound->play();

    //qDebug()<<"开始闯关";
    //按钮动画
    startBtn->zoom1();
    startBtn->zoom2();

    //延时进入场景2
    QTimer::singleShot(500,this,[=](){
        this->chooseScene->setGeometry(this->geometry());//同步场景位置
       this->chooseScene->show();
        this->hide();

    });

});
     //监听选关场景中的返回按钮信号
connect(this->chooseScene,&ChooseLevelScene::ChooseSceneBack,[=](){
    this->setGeometry(this->chooseScene->geometry());

    this->show();
     this->chooseScene->hide();

});


}
    //绘图事件
    void MainScene::paintEvent(QPaintEvent *){
        QPainter painter(this);
        QPixmap pix;
        pix.load(":/res/image/MenuSceneBg.png");
        //绘制背景
        painter.drawPixmap(0,0,this->width(),this->height(),pix);
        //加载标题资源
        pix.load(":/res/image/Title.png");
        //缩放图片
        //pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
        //绘制标题
        painter.drawPixmap(0,0,pix);
}








MainScene::~MainScene()
{
    delete ui;
}

